class_name UISimpleButton
extends Node2D

@export var hover_scale := 1.1
@export var tween_duration := 0.15
@onready var label: Label = $Label
var is_hover: bool = false
var level: Node2D = null
signal signal_on_click 
var is_enable: bool = false


func set_enable():
	is_enable = true
	

func set_disable():
	is_enable = false


func _process(delta: float):
	process_mouse_hover(delta)
	
	
func process_mouse_hover(delta: float):
	if not level: 
		level = get_tree().current_scene
		if not level: return
	
	var mouse_world_position = level.get_mouse_world_position()
	var button_rect = Rect2(global_position - label.size / 2.0, label.size)
	
	# 判断是否 hover
	if button_rect.has_point(mouse_world_position):
		if not is_hover:
			is_hover = true
			on_hover_enter()
	else:
		if is_hover:
			is_hover = false
			on_hover_exit()
			

func on_hover_enter():
	var tween = create_tween()
	tween.tween_property(self, "scale", Vector2(hover_scale, hover_scale), tween_duration)\
	.set_ease(Tween.EASE_IN_OUT).from_current()
	

func on_hover_exit():
	var tween = create_tween()
	tween.tween_property(self, "scale", Vector2(1.0, 1.0), tween_duration)\
	.set_ease(Tween.EASE_IN_OUT).from_current()


func _input(event):
	if not is_enable: return
	
	if event is InputEventMouseButton:
		if event.button_index == MOUSE_BUTTON_LEFT and event.pressed and is_hover:
			on_button_click()
	

func on_button_click():
	AudioManager.play_sound("sfx_menubuttonclick")
	signal_on_click.emit()
